﻿using System;
using System.Collections;
using System.Collections.Generic;

using QxFramework.Utilities;
using UnityEngine;

namespace QxFramework.Core
{
    /// <summary>
    /// 特效管理
    /// </summary>
    public class EffectsManager:ManagerBase<EffectsManager>
    {
        /// <summary>
        /// 特效预设目录，位于Assets/Resource/Prefabs/Effects/
        /// </summary>
        private readonly string FoldPath = "Prefabs/Effects/";
        public override void InitModules()
        {
            base.InitModules();
            Init();
        }
        /// <summary>
        /// 已经打开的特效列表
        /// </summary>
        private readonly Dictionary<string, EffectBase> _openEffects = new Dictionary<string, EffectBase>();

        /// <summary>
        /// 当前事件的ID的列表,用于判断哪个事件进入，哪个事件离开，并作出对应操作
        /// </summary>
        private List<int> currentEventIdList;

        /// <summary>
        /// 分组父对象
        /// </summary>
        private GameObject group;

        [Header("t特效显示优先级")]
        public int EffectOrder=10;

        /// <summary>
        /// 模块的初始化函数
        /// </summary>
        void Init()
        {
           
            //预加载预设
            var gos= ResourceManager.Instance.LoadAll<GameObject>(FoldPath);

            //提前生成缓存
            foreach (var item in gos)
            {
                ObjectPool.CreatePool(item,1);
            }


        }


        /// <summary>
        /// 退出时执行。
        /// </summary>
        public void Dispose()
        {
            {
                foreach (var kvp in _openEffects)
                {
                    if (kvp.Value!=null)
                    {
                        kvp.Value.OnClose();
                    }
                }
                currentEventIdList.Clear();
                _openEffects.Clear();
            }
        }

        /// <summary>
        /// 根据指定的事件去打开某个持续性特效预设
        /// </summary>
        /// <param name="ged">事件</param>
        /// <returns></returns>
        public EffectBase Open(string effectName,Vector3 position, string goName = "", object args = null)
        {
            if (group == null)
            {
                group = GameObject.Find("Effects");
                if (group == null)
                {
                    group = new GameObject { name = "Effects" };
                }
            }

            if (goName == "")
            {
                goName = effectName;
            }

            effectName = FoldPath+ effectName;
            GameObject effectGo;
            //如果是已经存在过的痕迹
            if (_openEffects.ContainsKey(goName))
            {
                effectGo = _openEffects[goName].gameObject;
            }
            else
            {
                effectGo = ResourceManager.Instance.Instantiate(effectName, group.transform);
                if (effectGo.GetComponent<Renderer>())
                {
                    effectGo.GetComponent<Renderer>().sortingOrder=EffectOrder;
                }
                //统一更改effect的layer
                if (effectGo.GetComponent<LineRenderer>())
                {
                    effectGo.GetComponent<LineRenderer>().sortingOrder = EffectOrder;
                }
                if (effectGo.GetComponent<SpriteRenderer>())
                {

                    effectGo.GetComponent<SpriteRenderer>().sortingOrder = EffectOrder;
                }
            }
            

            effectGo.transform.position = position;
            effectGo.name = goName;
            if (effectGo != null)
            {

                EffectBase effectBase;
                if (effectGo.TryGetComponent<EffectBase>(out effectBase))
                {
                    effectBase.OnDisplay(position,args);

                    _openEffects[goName] = effectBase;

                    return effectBase;
                }
                Debug.LogError("[EffectsManager] 未挂上继承EffectBase的脚本" + effectName);
                return null;
            }
            else
            {
                Debug.LogError("[EffectsManager] 找不到" + effectName);
                return null;
            }
        }

        /// <summary>
        /// 按对象关闭特效
        /// </summary>
        /// <param name="effectName"></param>
        public void CloseEffect(EffectBase eb)
        {
            if (eb != null)
            {
                //Debug.Log("关闭" + eb.name);
                eb.OnClose();
                if (_openEffects.ContainsKey(eb.gameObject.name))
                {
                    _openEffects.Remove(eb.gameObject.name);
                }
                ObjectPool.Recycle(eb.gameObject);
            }
        }

        /// <summary>
        /// 按名字关闭特效
        /// </summary>
        /// <param name="effectName"></param>
        public void CloseEffect(string effectName)
        {
            EffectBase eb;
            if (_openEffects.TryGetValue(effectName, out eb))
            {
                eb.OnClose();
                _openEffects.Remove(effectName);
                ObjectPool.Recycle(eb.gameObject);
            }

        }


        public enum EffectMsg
        {
            /// <summary>
            /// 扬尘特效
            /// </summary>
            Dust,
            /// <summary>
            /// 物理量改变特效
            /// </summary>
            Mogic,
            /// <summary>
            /// 有物理量被更新时
            /// </summary>
            Update,

        }
 /*       public class EffectMsgArgs : EventArgs
        {
            public object info;
            public float val;
            
            public EffectMsgArgs(float M,object obj = null)
            {
                info = obj;
                val = M;
            }
        }*/
    }
}